package com.itheima.report;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.ReportService;
import com.itheima.utils.POIUtils;
import com.itheima.utils.SMSUtils;
import org.apache.http.HttpResponse;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <h3>health94_pareant</h3>
 * <p></p>
 *
 * @author : hhw
 * @date : 2020-03-31 21:59
 **/
@RestController
@RequestMapping("report")
public class ReportController {
    @Reference
    private ReportService reportService;


    @RequestMapping("setMealReport")
    public Result setMealReport(){
        try {
            Map map = reportService.setMealReport();
            return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS, map);
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }
    @RequestMapping("businessReport")
    public Result businessReport(){
        try {
            Map map = reportService.businessReport();
            return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS, map);
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }
    @RequestMapping("exportExcel")
    @ResponseStatus(code = HttpStatus.OK)
    public void exportExcel(HttpSession session, HttpServletResponse response){
        try {
            String tempalatePath = session.getServletContext().getRealPath("template") + File.separator + "report_template.xlsx";
            XSSFWorkbook workbook = new XSSFWorkbook(tempalatePath);
            Map map = reportService.businessReport();
            //获得第0个工作表
            XSSFSheet sheet = workbook.getSheetAt(0);

            XSSFRow row = sheet.getRow(2);
            row.getCell(5).setCellValue((String)map.get("reportDate"));

            row = sheet.getRow(4);
            row.getCell(5).setCellValue((Integer)map.get("todayNewMember"));
            row.getCell(7).setCellValue((Integer)map.get("totalMember"));

            row = sheet.getRow(5);
            row.getCell(5).setCellValue((Integer)map.get("thisWeekNewMember"));
            row.getCell(7).setCellValue((Integer)map.get("thisMonthNewMember"));

            row = sheet.getRow(7);
            row.getCell(5).setCellValue((Integer)map.get("todayOrderNumber"));
            row.getCell(7).setCellValue((Integer)map.get("todayVisitsNumber"));

            row = sheet.getRow(8);
            row.getCell(5).setCellValue((Integer)map.get("thisWeekOrderNumber"));
            row.getCell(7).setCellValue((Integer)map.get("thisWeekVisitsNumber"));

            row = sheet.getRow(9);
            row.getCell(5).setCellValue((Integer)map.get("thisMonthOrderNumber"));
            row.getCell(7).setCellValue((Integer)map.get("thisMonthVisitsNumber"));

            List setmeal = (List) map.get("hotSetmeal");
            int inital = 11;
            for (int i = 0; i < setmeal.size(); i++) {
                row = sheet.getRow(inital);
                Map map1 = (Map) setmeal.get(i);
                row.getCell(4).setCellValue((String)map1.get("name"));
                row.getCell(5).setCellValue((Long)map1.get("setmeal_count"));
                //这个是bigdecimal
                row.getCell(6).setCellValue(map1.get("proportion").toString());
                inital += 1;
            }
            //设置响应的文件格式
            response.setContentType("application/vnd.ms-excel");
            //设置下载方式,以附件形式下载
            response.setHeader("content-Disposition","attachment;filename=report.xlsx");
            ServletOutputStream outputStream = response.getOutputStream();

            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
            workbook.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
